package com.cheney.demo;

import org.junit.Test;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;


public class AESUtil {

    @Test
    public void test() throws Exception {
        String res = Decrypt("T9L5c0LmLrychmocF37Yx3Bp6lGQ5Ynh\\/Glzh+q4tM6L4emwsDep7MSwm+jD+szvCNxw121Vg\\/pV\\/TyMlwmSVRtI0wkwHksaX9BjpFhqK9UNiF28vFxxtGZAasnszT7ce9MTVxcVvqvWc1Zh32JOZnQAFq\\/HyGMl7XpCxeeqPfADeGG6\\/0ZQCwRfmcmFLhATzO\\/F\\/9jbp0gqVhCmthh4SS0t982sPsHKCh6HrZDN+jQjzHmmsuj20GEOoc3\\/QpX5sTMk3xSm0XHB\\/GogXI5kcwREVy\\/K5RKuqCMIdnVMbP6ZCaCzw0p9btYJuvAgG1xvHngV5RzaijfMq3ijGXbft+BdxsTGBfeXeCCyuCDhTgrmc4MdEGdIEuntsjJNj8rOFLMPpZ5Q6dRXemaKGmaxqkfUXTqhHKVy5F05Bz0nIVUD\\/lBSuFv8E9qNPvVjM9LLATgpK8ZZea7mgiz0J7JkIRHbbXf7IDt\\/taTgmFTd+iddBw8pjyu9Cj5Q9zsVGvyEfN4ibMGQznbaZJR9TbTsQ1g\\/ndgczUQvv6nBMp6k1XCktXXNXQFJ7000X4fkMnUYAlqvMjdkLgI121RL6wxTRINkYlGdpJQJQ11x3EvJV4Yp\\/K3vBYQRfbmewjkZ5ytu\\/q4E3cz4VepaMOKbXOy6HkqKBSBlOehESdtH\\/0ORPeHFflI4HILJ+ggv9gcdhnumbGUPJ4GxOSieV7f3jNI6xG61f+Wo3Rq56FUyqtlqwvPBXEX7K9TXE4MP3EqBw3r1m17soCacANZAiFO8wrgOrNi1bYCmjJJcvewDfx0K1yJRZSURVp12r9rgD7T5F94fAEaXltg\\/iV\\/2WcxhpkqpDcJPbjDg2AIBqZQM8amFcWNUErXOYDGviSxeflgbZj5GtIO+2aag\\/yxH3QxcmB76UaIcYSxA7JnH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9hAOWo\\/WgDNHjAqXhDe7d+etdMKFIc2Za2mMqHcp4IIXZIXR0dFx53965oKu\\/5gpn0VgQ8QHQ5euu5xU2\\/YcvSvORLzfmt86ZjrvFVW8T4V3j95RYm8pccXlFh76Xv7j0ELNQtcyD7UJueXjyRVO4QwzL7uOtWzX4ICZssBR1\\/Rkl7uM6IVBYxhCqDeYN7AafP9qPBA1bXtUVdxATkxmXd3IPc+9YxMNlRLfIvORmgglTB3BZD2Bpfo8nlL8VYYePrqCvUPgMM5iR6E6h4BZwdNN4p5yBnvg0IyGPg4gFeI+1MujrGIhuYU6Iifr0JDc\\/ibC0wpIOqii+Gjk3kEEnsuvsx3bNqo6T9BP5pfLgacVKZghQH\\/tL1KJXLFFMIFWIwKYUxQjDM1seqdzDqYQh7RAR3bbJFpUWN9GU26qFCpVAxk6IpvEAh67tP5hf996ELv\\/N8nU9+yLXOln+LdrWCvskQ4OBEjNlqEFko3CMTkpggtfQayyQiKyDr5voXouCvO+qYEww6Ut+cTczItuSBoJ1SbvyDBDs1sC9Gwp9m\\/qa4EYtOc2ZTxTdzxrF5dt5YqaAYRZ243wZPo834+PGp9RwLxbp75T1afErJY+ZHxG9tmu7brTJNM0\\/Nk8G\\/OMmceQEaNCZYMLEAekPb3MaAHSkDzbtSK1\\/67vBELh8PMiVBaLB\\/LmC+U5A9ShL46NBYGRlL5p+dRPGxTsV\\/JAP6N46g1jMw+WO0fXmSefx6yk2SpkbxWgv2UfWd4W1PLdR0vVfmaiq9Ff\\/e35Ed91U7MwIzx6vF5bgWAcWGAfLypDn\\/SgoZBZqpEY4gp4fXU4cJz9xcoOa7mXb86GNFlyAMLh29yAzjWCYl7fEUqUySGXL9op87UasRrch2CFMqdgZYokkbO6zcVXJ5+BiXO2zhhDmNgBiaI", "c400db3363b99ee026bc9ed0741b79df");
        System.out.println(res);
    }

    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes(), "AES");

        Cipher cipher;
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.getEncoder().encodeToString(encrypted);
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes(), "AES");

            Cipher cipher;
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.getDecoder().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
}